package com.ruoyi.web.controller.system;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysTextbook;
import com.ruoyi.system.service.ISysTextbookService;

/**
 * 教材管理Controller
 * 
 * @author Jason
 */
@RestController
@RequestMapping("/system/textbook")
public class SysTextbookController extends BaseController
{
    @Autowired
    private ISysTextbookService sysTextbookService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:textbook:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTextbook textbook)
    {
        startPage();
        List<SysTextbook> list = sysTextbookService.selectList(textbook);
        return getDataTable(list);
    }

    @Log(title = "教材管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:textbook:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTextbook entity)
    {
        List<SysTextbook> list = sysTextbookService.selectList(entity);
        ExcelUtil<SysTextbook> util = new ExcelUtil<SysTextbook>(SysTextbook.class);
        util.exportExcel(response, list, "教材数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:textbook:list')")
    @GetMapping(value = "/{textbookId}")
    public AjaxResult getInfo(@PathVariable("textbookId") Long textbookId)
    {
        return success(sysTextbookService.selectById(textbookId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:textbook:add')")
    @Log(title = "教材管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTextbook textbook)
    {
        return toAjax(sysTextbookService.insert(textbook));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('system:textbook:edit')")
    @Log(title = "教材管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTextbook textbook)
    {
        return toAjax(sysTextbookService.update(textbook));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('system:textbook:remove')")
    @Log(title = "教材管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{textbookIds}")
    public AjaxResult remove(@PathVariable Long[] textbookIds)
    {
        return toAjax(sysTextbookService.deleteByIds(textbookIds));
    }

    @Log(title = "教材管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:textbook:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<SysTextbook> util = new ExcelUtil<SysTextbook>(SysTextbook.class);
        List<SysTextbook> listEntity = util.importExcel(file.getInputStream());
        String message = sysTextbookService.importData(listEntity);
        return success(message);
    }
}